<?php
/**
 * Created by PhpStorm.
 * User: lyw
 * Date: 2016/5/18
 * Time: 11:34
 */

class Case_manageController extends BaseController {
    function index(){
        $do = new Do病历文章();
        $doComment = new DoStatistic();
        $title = $this->request('title');
        $where = '';
        if($title){
            $where = " a.title like '%".$title."%'";
            $count = $do->where("title like '%".$title."%'")->count();
        }else{
            $count = $do->count();
        }
        $page = new \tools\Page($count, 15);
        $case = $do->fields('a.*')->offset($page->limit)
            ->limit($page->pagesize)
            ->where($where)
            ->orderby("a.添加时间 desc")->rows();
        foreach ($case as $k => $v) {
            $doUser = new Do用户();
            $doUser->set_id($v['添加用户id']);
            $doUser->set_公众号ID(G_SERV_ID);
            $doUser->fields("name")->where($doUser->data)->row();
            $case[$k]['username'] =  $doUser->get_name();
            $case[$k]['num'] =  $doComment->where("`type` = 1 AND `tid` = ".$v['id'])->count();
        }
        require $this->template;
    }
    function show_user(){
        $caseId = $_GET['id'];
        if($_POST['action'])
        {
            $user = new Do用户();
            $doUser = $user->fields("id,name,telephone,头像")->where("name like '%".$_POST['name']."%' and 公众号ID=".G_SERV_ID)->rows();
        }
        require $this->template;
    }

    function update_user(){
        $caseId = $_GET['id'];
        $userId = $_GET['userId'];
        $doCase = new Do病历文章();
        $doCase->set_id($caseId);
        $doCase->set_添加用户id($userId);
        $doCase->save();
        $this->redirect('case_manage','index');
    }

    function test() {

        $do消息记录 =  new Do消息发送记录();
        $doUser = new Do用户();
        $do医师详情 = new Do医师详情();

        $do消息记录->fields("a.*, u.name, y.*");
        $do消息记录->leftJoin($doUser, 'u', 'a.接收消息用户openID = u.openID');
        $do消息记录->leftJoin($do医师详情, 'y', 'u.id = y.用户id');

        $rows = $do消息记录->limit(3)->count();

        echo $do消息记录->sql;

        print_r($rows);

        #http://localhost/aibeieryi/tianshi3/admin/index.php?m=case_manage&a=test

        /*
        for ($i = 0; $i < 800; $i++) {
            sleep(1);
            echo $i;
        }
        if (!empty($_POST)) {
            $handle = fopen('ttt.txt', 'a+');
            for ($i = 0; $i < 3; $i++) {
                sleep(1);
                fwrite($handle, "$i\n");
                echo $i;
            }
            fclose($handle);
        } else {
            require_once $this->template;
        } */

    }
    function export_case(){
        $do = new Do病历文章();
        $doComment = new DoStatistic();

        $case = $do->fields('*')
            ->orderby("添加时间 desc")->rows();
        $data[1]=array('病例文章主题' ,
            '作者',
            '阅读量',
            '评论人数',
            '阅读人数',
            '添加时间');
        foreach ($case as $k => $v) {
            $doUser = new Do用户();
            $doUser->set_id($v['添加用户id']);
            $doUser->set_公众号ID(G_SERV_ID);
            $doUser->fields("name")->where($doUser->data)->row();
            $case[$k]['username'] =  $doUser->get_name();
            $case[$k]['num'] =  $doComment->where("`type` = 1 AND `tid` = ".$v['id'])->count();

            $data[]=array("".$case[$k]['title']."",
                "".$case[$k]['username']."",
                "".$case[$k]['浏览次数']."",
                "".$case[$k]['评论次数']."",
                "".$case[$k]['num']."",
                "".$case[$k]['添加时间']."");
        }
        Tools::export('case',$data);
    }
    public function release_case(){
        $id = $this->request("id");
        $doCase = new Do病历文章();
        $doCase->set_id($id);
        $case = $doCase->fields("id, title, 详情 detail, 关键词 keyWords, 添加用户id")->where($doCase->data)->row()->data;
        $doUser = new Do医师详情();
        $doUser->set_用户id($case['添加用户id']);
        $doctor = $doUser->fields("name, 医院, 用户id")->where($doUser->data)->row()->data;
        require $this->template;
    }

    public function release_case_post(){
        $id = $this->request("id");
        $title = $this->request("title", '', true);
        $keyWords = $this->request("keyWords");
        $content = $this->request("content");
        $userId = $this->request("专家id");
        $doCase = new Do病历文章();
        $doCase->set_title($title);
        $doCase->set_关键词($keyWords);
        $doCase->set_详情($content);
        $doCase->set_添加用户id($userId);
        if($id){
            $doCase->set_id($id);
        }
        if($doCase->save()){
            $msg = ($id) ? '修改成功' : '添加成功';
            $url = $this->url('case_manage','index');
            $this->successPage($msg, $url);
        } else {
            $msg = ($id) ? '修改失败' : '添加失败';
            $this->errorPage($msg);
        }
    }

}